package com.mttqq.family.core;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;


/*******************************************************************************
 * <b>类名：InitConfig</b> <br/>
 * 功能：加载配置文件<br/>
 * 日期: 2012-01-11 <br/>
 * 
 * @author 谢广泉  xiegqooo@hotmail.com
 * @version 1.0.0
 * 
 ******************************************************************************/
public class InitConfig extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private final Logger logger = Logger.getLogger(InitConfig.class);
	
	private static SqlSessionFactory sqlSessionFactory;
	
	//private static SqlSession sqlSession;

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {

		// 设置项目参数
		String basedir = config.getServletContext().getRealPath("/");
		System.setProperty("app.path", basedir);

		// 项目名称
		String pname = config.getServletContext().getContextPath();
		System.setProperty("app.name", pname.substring(1));

		// 项目类路径
		String classpath = InitConfig.class.getResource("/").getPath();

		System.out.println("项目路径：" + basedir);
		System.out.println("项目名称：" + pname);
		System.out.println("项目类路径：" + classpath);

		// 路径前缀
		String prefix = basedir;

		// web 属性值
		String log4jPropertiesFileUrl = null;
		String myibatisconfig = null;

		// 读取文件 web.xml 配置内容
		String log4jpath = config.getInitParameter("log4jUrl");
		if (log4jpath != null) {
			log4jPropertiesFileUrl = prefix + log4jpath;
		}


		String myibatispath = config.getInitParameter("myibatisUrl");
		if (myibatispath != null) {
			myibatisconfig = prefix + myibatispath;
		}

		/*
		 * 加载log4j配置文件
		 */
		if (log4jPropertiesFileUrl != null) {
			PropertyConfigurator.configure(log4jPropertiesFileUrl);

			if (logger.isInfoEnabled()) {
				logger.info("Init log4j properties succeed...");
			}
		} else {
			echo(" --------------------------------------------------------- ");
			echo(" --- Try to find the properties file of LOG4J Failed...");
			echo(" --- Please check whether your properties file of LOG4J in your "
							+ log4jPropertiesFileUrl + " exist.");
			echo(" --------------------------------------------------------- ");
		}

		/**
		 * 加载 myibatis配置文件
		 */
		if (myibatisconfig != null) {
			try {
				Reader reader = new FileReader(myibatisconfig);
				sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
				
			} catch (FileNotFoundException e) {
				echo(" --------------------------------------------------------- ");
				echo(" --- 加载  myibatis 失败 ! ---");
							e.printStackTrace();
				echo(" --------------------------------------------------------- ");
			}
		} else {
			echo(" --------------------------------------------------------- ");
			echo(" --- Try to find the properties file of IBATIS Failed...");
			echo(" --- Please check whether your properties file of MyIbatis in your "
							+ myibatisconfig + " exist.");
			echo(" --------------------------------------------------------- ");
		}

	}
	
	public void echo(String s){
		System.err.println(s);
	}
	
	public static SqlSessionFactory getSqlSessionFactory() {
		return sqlSessionFactory;
	}
}

